<template>
	<view>
		<!-- 蓝牙开关 -->
		<view class="switchContainer">
			<view>{{$t('my.set.blueTooth')}}</view>
			<switch :checked="checked" @change="switchChange" />
		</view>
		<view v-if="checked">
			<!-- 已连接蓝牙 -->
			<view class="cellContainer" v-if="this.connectedDevices.length!==0">
				<zy-cell :cellList="connectedDevices" :title="$t('my.set.pairedDevice')" />
			</view>
			<!-- 已配对设备删除弹窗 -->
			<zy-popup ref="delpop" mode="bottom" closeable slotContentShow>
				<zy-button type="danger" plain>{{$t('my.set.ignoreDevice')}}</zy-button>
				<zy-button @click="$refs.delpop.close()" type="primary" plain>{{$t('my.set.cancel')}}</zy-button>
			</zy-popup>
			<!-- 获取蓝牙列表 -->
			<view class="cellContainer">
				<zy-cell :cellList="devices" :title="$t('my.set.devices')" @clickHandler='clickListHandler' />
			</view>
		</view>
	</view>
</template>

<script>
	import blueTooth from '@/common/blueTooth.js'
	export default {
		data() {
			return {
				checked: false, //蓝牙开关是否打开
				connectedDevices: [], //连接过的蓝牙列表数据
				delconnectedDevicesData: {},
				devices: [], //获取蓝牙列表数据
			}
		},
		onLoad() {
			// 判断蓝牙是否打开
			if (uni.getStorageSync('blueToothSwitchChecked')) {
				this.checked = true
				this.switchChange()
				// 判断是否存在连接过的蓝牙
				if (uni.getStorageSync("blueToothConnectedDevices")) {
					this.connectedDevices = uni.getStorageSync("blueToothConnectedDevices")
				}
			}
		},
		methods: {
			// 蓝牙开关事件
			switchChange(val) {
				var that = this
				that.devices = []
				if (val) {
					that.checked = val.detail.value
					uni.setStorageSync('blueToothSwitchChecked', that.checked)
				}
				if (that.checked) {
					//开始搜寻附近的蓝牙外围设备
					blueTooth.openBluetoothAdapter().then(() => {
						// 监听寻找到新设备的事件
						uni.onBluetoothDeviceFound(function(data) {
							var name = data.devices[0].name
							var deviceId = data.devices[0].deviceId
							if (name) {
								if (val) {
									that.checked = val.detail.value
									uni.setStorageSync('blueToothSwitchChecked', that.checked)
									setTimeout(() => {
										that.connectedDevices = uni.getStorageSync(
											'blueToothConnectedDevices')
										if (that.connectedDevices.length > 0) {
											that.connectedDevices.forEach((item, index) => {
												if (name == item.text)
													that.clickListHandler({
														item: item
													})
											})
										}
									}, 1000)
								}
								// 设备单元格列表获取数据
								that.devices.push({
									text: name,
									deviceId: deviceId,
									services: []
								})
							}
						})
					}).catch((res) => {
						blueTooth.bluetoothAdapter().then(() => {
							that.switchChange({
								detail: {
									value: true
								}
							})
						})
					})
				} else {
					blueTooth.closeBluetoothAdapter()
					// 退出蓝牙
					that.devices = []
					uni.closeBluetoothAdapter({
						success(res) {
							// 设置关闭蓝牙后,已连接的蓝牙都失效
							let arr = uni.getStorageSync('blueToothConnectedDevices')
							for (var i = 0; i < arr.length; i++) {
								arr[i].sufText = this.$t('my.set.unconnect')
							}
							uni.setStorageSync('blueToothConnectedDevices', arr)
							uni.removeStorageSync('blueToothDeviceId')
							uni.removeStorageSync("blueToothServiceId");
							uni.removeStorageSync("blueToothCharacteristicId");
						}
					})
				}
			},
			// 连接蓝牙
			clickListHandler(e) {
				let that = this
				uni.showLoading({
					title:this.$t('my.set.blueToothConnecting')
				});
				blueTooth.createBLEConnection(e).then((res) => {
					blueTooth.getBLEServices(e).then((res) => {
						that.connectedDevices = res
						//把已经连接的蓝牙设备信息放入缓存
						uni.setStorageSync("blueToothConnectedDevices", that
							.connectedDevices)
						blueTooth.openBluetoothAdapter().then(() => {
							that.devices = []
							// 监听寻找到新设备的事件
							uni.onBluetoothDeviceFound(function(data) {
								var name = data.devices[0].name
								var deviceId = data.devices[0].deviceId
								if (name) {
									// 设备单元格列表获取数据
									that.devices.push({
										text: name,
										deviceId: deviceId,
										services: []
									})
								}
							})
						})
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.switchContainer {
		display: flex;
		justify-content: space-between;
		width: calc(100% - 80rpx);
		margin: 20rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
	}

	.switchContainer view {
		margin: auto 0;
		padding-left: 30rpx;
	}

	.connectedContainer {
		margin: 20rpx 0;
		padding: 20rpx;
		text-align: left;
		background-color: #f5f5f5;
		color: #ff0000;
		border-radius: 10rpx;
	}

	.cellContainer {
		background-color: #f5f5f5;
		border-radius: 10rpx;
		padding: 10rpx 0;
		margin: 10rpx 0;
		text-align: left !important;
	}
</style>